/**
 * Filename: PopMenu.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/6/19 17:28
 */
package com.github.postsanf.yinian.activity.pop;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.UIHelper;

/**
 * 菜单弹窗
 */
public class PopMenuTimeLine extends PopupWindow {
    private Context mContext;
    // 列表弹窗的间隔
    protected final int LIST_PADDING = 10;
    // 实例化一个矩形
    private Rect mRect = new Rect();
    // 坐标的位置（x、y）
    private final int[] mLocation = new int[2];
    // 屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;
    // 判断是否需要添加或更新列表子类项
    private boolean mIsDirty;
    // 位置不在中心
    private int popupGravity = Gravity.NO_GRAVITY;
    private ImageView timeline_face1, timeline_face2, timeline_face3, timeline_face4, timeline_face5;

    public PopMenuTimeLine(Context context) {
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public PopMenuTimeLine(Context context, int width, int height) {
        this.mContext = context;
        // 设置可以获得焦点
        setFocusable(true);
        // 设置弹窗内可点击
        setTouchable(true);
        // 设置弹窗外可点击
        setOutsideTouchable(true);
        // 获得屏幕的宽度和高度
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mScreenWidth = wm.getDefaultDisplay().getWidth();
        mScreenHeight = wm.getDefaultDisplay().getHeight();
        // 设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);
        setBackgroundDrawable(new BitmapDrawable());
        // 设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_timeline, null));
        setAnimationStyle(R.style.AnimHead);
        initUI();
    }

    private void initUI() {
        timeline_face1 = (ImageView) getContentView().findViewById(R.id.timeline_face1);
        timeline_face2 = (ImageView) getContentView().findViewById(R.id.timeline_face2);
        timeline_face3 = (ImageView) getContentView().findViewById(R.id.timeline_face3);
        timeline_face4 = (ImageView) getContentView().findViewById(R.id.timeline_face4);
        timeline_face5 = (ImageView) getContentView().findViewById(R.id.timeline_face5);
    }

    /**
     * 显示弹窗列表界面
     */
    public void show(View view) {
        // 获得点击屏幕的位置坐标
        view.getLocationOnScreen(mLocation);
        // 设置矩形的大小
        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
                mLocation[1] + view.getHeight());
        // 显示弹窗的位置
//        timeline_face1.setVisibility(View.INVISIBLE);
//        timeline_face2.setVisibility(View.INVISIBLE);
//        timeline_face3.setVisibility(View.INVISIBLE);
//        timeline_face4.setVisibility(View.INVISIBLE);
//        timeline_face5.setVisibility(View.INVISIBLE);
        final ScaleAnimation scaleAnimation1 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.5f);
        scaleAnimation1.setDuration(80);
        final ScaleAnimation scaleAnimation2 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation2.setDuration(80);
        final ScaleAnimation scaleAnimation3 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation3.setDuration(80);
        final ScaleAnimation scaleAnimation4 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation4.setDuration(80);
        final ScaleAnimation scaleAnimation5 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation5.setDuration(80);
        timeline_face5.startAnimation(scaleAnimation1);
        scaleAnimation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        timeline_face4.startAnimation(scaleAnimation2);
                    }
                }, 40);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                timeline_face5.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        scaleAnimation2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        timeline_face3.startAnimation(scaleAnimation3);
                    }
                }, 40);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                timeline_face4.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        scaleAnimation3.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        timeline_face2.startAnimation(scaleAnimation4);
                    }
                }, 40);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                timeline_face3.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        scaleAnimation4.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        timeline_face1.startAnimation(scaleAnimation5);
                    }
                }, 40);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                timeline_face2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        scaleAnimation5.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                timeline_face1.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        showAtLocation(view, popupGravity, mScreenWidth - LIST_PADDING
                - (getWidth()) - UIHelper.dip2px(mContext, 5), mRect.bottom + UIHelper.dip2px(mContext, 7));
    }


}